package com.trace.meat.controller;

import com.trace.meat.entity.OrderTbl;
import com.trace.meat.entity.Result;
import com.trace.meat.entityModel.AnimalModel;
import com.trace.meat.entityModel.ChainModel;
import com.trace.meat.entityModel.EntModel;
import com.trace.meat.entityModel.ProductModel;
import com.trace.meat.service.AnimalService;
import com.trace.meat.service.EntService;
import com.trace.meat.service.OrderTblService;
import com.trace.meat.service.ProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * (OrderTbl)表控制层
 *
 * @author makejava
 * @since 2021-06-19 14:14:03
 */
@RestController
@RequestMapping("orderTbl")
public class OrderTblController {
    /**
     * 服务对象
     */
    @Resource
    private OrderTblService orderTblService;
    @Resource
    private AnimalService animalService;
    @Resource
    private ProductService productService;
    @Resource
    private EntController entController;
    @Resource
    private EntService entService;
    /**
     * 通过主键查询单条数据
     *
     * @param oid 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public OrderTbl selectOne(Integer oid) {
        return this.orderTblService.queryById(oid);
    }
    /**
     * 生成订单链条
     *
     * @param gid 主键
     * @param type 货物类型
     * @return 订单链条
     */
    @GetMapping("ChainForm")
    public Result  Chain_Form(Integer gid,Integer type){
        Result result=new Result();
        result.setDetail(null);
        result.setSuccess(false);
        Integer id;
        if(type==0)//整猪
        {
            id=animalService.queryById(gid).getLastOid();
            if(orderTblService.queryById(id)==null){
                result.setMsg("牲畜信息记录有误");
                return result;
            }
        }
        else if(type==1)//切割后
        {
            id=productService.queryById(gid).getLastOid();
            if(orderTblService.queryById(id)==null){
                result.setMsg("产品信息记录有误");
                return result;
            }
        }
        else{
            result.setMsg("输入类型错误");
            return result;
        }
        OrderTbl tempOT=this.orderTblService.queryById(id);
        List<OrderTbl> List_OT=new ArrayList<OrderTbl>();
        List_OT.add(tempOT);
        while(tempOT.getLastOid()!=null)
        {
            tempOT=this.orderTblService.queryById(tempOT.getLastOid());
            List_OT.add(0,tempOT);
        }
        Iterator<OrderTbl> iterator= List_OT.listIterator();
        while(iterator.hasNext())
        {
            System.out.println(iterator.next().getOid());
        }
        List<ChainModel> chain=new ArrayList<ChainModel>();
        Iterator it=List_OT.listIterator();
        while (it.hasNext())
        {
            ChainModel chainModel=new ChainModel();
            OrderTbl orderTbl=(OrderTbl)it.next();
            chainModel.setEid(orderTbl.getOutEid());
            EntModel entModel1=entService.queryModelById(chainModel.getEid());
            chainModel.setEname(entModel1.getEname());
            chainModel.setEtype(entModel1.getEtype());
            chainModel.setEid(orderTbl.getOutEid());
            chainModel.setTime(orderTbl.getConfirmTime());
            chain.add(chainModel);
            if(!it.hasNext())
            {
                ChainModel temp=new ChainModel(chainModel);
                temp.setEid(orderTbl.getInEid());
                EntModel entModel2=entService.queryModelById(temp.getEid());
                temp.setEtype(entModel2.getEtype());
                temp.setEname(entModel2.getEname());
                if(type==0)//整只
                {
                    AnimalModel animalModel=animalService.queryModelById(orderTbl.getGid());
                    result.setInfo(animalModel);
                }
                else if(type==1)//切割
                {
                    ProductModel productModel=productService.queryModelById(orderTbl.getGid());
                    result.setInfo(productModel);
                }
                chain.add(temp);
            }
        }
        result.setDetail(chain);
        result.setMsg("链条合成成功");
        result.setSuccess(true);
        return result;
    }
    @GetMapping("insertOne")
    public OrderTbl insert(Integer oeid,Integer ieid,Integer lastoid,Integer gid,short gtype,String ctime,String plate,String info) {
        OrderTbl orderTbl=new OrderTbl();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(ctime);
        } catch (ParseException e) {
            System.out.println("时间输入格式错误");
            e.printStackTrace();
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
        }
        if(!lastoid.equals(0))
        {
            orderTbl.setLastOid(lastoid);
        }
        if (info!=null)
        {
            orderTbl.setInfo(info);
        }
        orderTbl.setGtype(gtype);
        orderTbl.setInEid(ieid);
        orderTbl.setGid(gid);
        orderTbl.setOutEid(oeid);
        orderTbl.setPlate(plate);
        orderTbl.setConfirmTime(ctime);
        return orderTblService.insert(orderTbl);
    }
    /**
     * 通过主键删除数据
     *
     * @param oid 主键
     * @return 是否成功
     */
    @GetMapping("deleteOne")
    public boolean deleteById(Integer oid) {
        return this.orderTblService.deleteById(oid);
    }
    /**
     * 修改数据
     */
    //http://127.0.0.1:11451/orderTbl/updateOne?oid=1
    @GetMapping("updateOne")
    public OrderTbl update(Integer oid, Integer outEid, Integer inEid,Integer lastOid,Integer gid,Short gtype, String confirmTime,String plate,String info) {
        OrderTbl orderTbl=new OrderTbl();
        orderTbl.setOid(oid);
        orderTbl.setOutEid(outEid);
        orderTbl.setInEid(inEid);
        orderTbl.setLastOid(lastOid);
        orderTbl.setGid(gid);
        orderTbl.setGtype(gtype);
        orderTbl.setConfirmTime(confirmTime);
        orderTbl.setPlate(plate);
        orderTbl.setInfo(info);
        return this.orderTblService.update(orderTbl);
    }

}


